<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/4
 * Time: 12:44
 */
namespace app\home\controller;
use think\Controller;
use think\Request;
use think\Session;
// 允许所有跨域请求
header('Access-Control-Allow-Origin: *');
class Login extends Controller
{
    public function login(){
        if(Request::instance()->isPost()){
            $name = input('post.name');
            $pass = md5(input('post.pass'));
            $where['name'] = $name;
            $where['pass'] = $pass;
            $user= db('user')->where($where)->find();
            if($user){
                //判断是否关闭网站
                $webconfig = db('webconfig')->where('id',1)->find();
                if($webconfig['web_open'] != 1){
                    return json_encode(['msg'=>2,'info'=>$webconfig['web_text']]);
                }
                //账号是否冻结
                if($user['state'] != 1){
                    return json_encode(['msg'=>0,'info'=>'账号已被冻结']);
                }
                $date = date("Y-m-d H:i:s");
                Session::set('user',$user);
                $data['login_ip'] = Request::instance()->ip();
                $data['last_time'] = $date;
                $data['on_line'] = time() + 600;
                db('user')->where('id',$user['id'])->update($data);
                return json_encode(['msg'=>1,'info'=>'登录成功!']);
            }else{
                return json_encode(['msg'=>0,'info'=>'账号或密码错误']);
            }
        }else{
            return $this->fetch('common/login');
        }

    }
    
    public function outlogin(){
        Session::delete("user");
        return $this->fetch("common/login");
    }
}


